/*
	检测登录状态模块
	后续进行整合
	依赖：
		jquery
		layui
		common.js
*/

//获取cookies
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var adminUseInfo = Cookies.getJSON('kplus_ad_use_info');
//console.log(adminLoginedInfo)

//使用layer
layui.use('layer', function(){
	var layer = layui.layer;

	if(adminLoginedInfo){   //admin登录信息存在

		adminLoginFromLoginedInfo(adminLoginedInfo);

	}else{   //登录信息不存在
		if(Cookies.getJSON('kplus_ad_logined_info')){   //Cookies 内容存在

			var libAdminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info')
			adminLoginFromLoginedInfo(libAdminLoginedInfo);

		}else{
			//Cookies 内容不存在 
			layui.layer.msg(MESSAGES.Logon_failure, {time: 2000, end: function(){  //销毁时回调
				location.href = './login.html';
			}});
		}
	}

	//从登录信息登录
	function adminLoginFromLoginedInfo(adminLoginedInfo){
		var ali = adminLoginedInfo;
		//console.log(ali)
		var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
		$.ajax({
			url: BASE_SERVICEURL + URlS.admin,
			type:'POST',
			data: {
				aId: ali.aId,
				aToken: ali.aToken
			},
			success: function(responseData){
				//关闭load
				layer.close(loadIndex);
				if(responseData.msg.code == 200){
					//layer.msg(MESSAGES.welcome_admin + adminUseInfo.aName, {time: 2000});
					
					//存在触动行为，更新登录信息
	                var kplus_ad_logined_info = {
	                	aId: responseData.data.aId,
	                	aToken: responseData.data.aToken   
	                }
	                Cookies.set('kplus_ad_logined_info', kplus_ad_logined_info, {path: WEB_SETTING_OPTIONS.cookies_save_path})

				}else{
					layer.msg(MESSAGES.info_checkout_failure + responseData.msg.msg , {time: 2000});
					//跳转登录页
					location.href = './login.html';
				}
			},
			error: function(responseData){
				layer.close(loadIndex);
				layer.msg(MESSAGES.service_error, {time: 2000});
				//跳转500页面
				location.href = './500.html';
			}
		})
	}




})









